<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/23 0023
 * Time: 16:25
 */

namespace app\wap\controller;
use think\Db;
use think\Cache;

class PriceRemind extends Vip
{
    //todo 获取平台币种
    public function GetPlatformCoin(){
        	$list = Db::name('currency')
                ->where(['pid'=>0])
                ->field('cu_id,title as platform')
                ->select();       
            if(!$list)return $this->returnError($this->NoData);
            foreach ($list as $k=>$v){
                $res = Db::name('currency')->where(['pid'=>$v['cu_id'],'type'=>1])->field('cu_id,title')->select();    	
                if(!$res) continue;
                $ref = [];
                foreach ($res as $ke=>$va){
                    $nowprice = Db::name('timelydata')->where(['cu_id'=>$va['cu_id']])->order('time desc')->value('price');		
                    if(!$nowprice){
                        unset($res[$k]);
                        continue;
                    }
                  	if($nowprice > 10)$nowprice = round($nowprice,2);
                  	if($nowprice > 0.1)$nowprice = round($nowprice,3);
                  	if($nowprice < 0.1)$nowprice = round($nowprice,4);
                    $ref[] = [
                        'CNY'   => $this->ChangeCNY($nowprice),
                        'price' => $nowprice,
                        'cu_id' =>$va['cu_id'],
                        'title' =>strtoupper($va['title']),
                    ];
                }             	
                $list[$k]['son'] = $ref;
                unset($list[$k]['cu_id']);
            }
        return $this->returnSuccess($list);
    } 
    //todo 高低提醒列表
    public function HeightRemindList(){
        $map = ['a.uid'=>$this->uid,];
        $res = Db::name('height_remind')
            ->alias('a')
            ->join('currency b','a.cu_id=b.cu_id')
            ->join('currency c','b.pid=c.cu_id')
            ->field('a.cu_id,a.id,b.title,c.title as platform')
            ->where($map)
            ->select();
           	
        if($res){
            foreach ($res as $k=>$v){
                $price = Db::name('timelydata')->where(['title'=>$v['title'],'platform'=>$v['platform']])->value('price');
                $res[$k]['title'] = strtoupper($v['title']);
                $res[$k]['platform'] = $v['platform'];
                $res[$k]['price'] = round($price,4);
                $res[$k]['CNY'] = $this->ChangeCNY($price);
                $wprice = Db::name('jiage')->where(['platform'=>$v['platform'],'title'=>$v['title']])->value('price');
                $res[$k]['lv']  = ($price - $wprice)/$wprice;
            }
        }
        return $this->returnSuccess($res);
    }
    //todo 搜索币种
    public function SearchCoin(){
        $map = ['a.title'=>['like','%'.$this->data['title'].'%'],'a.pid'=>['>',0],'a.type'=>1];
        $data=	unserialize($this->redis->get('CurrencyList'));
      	
        $son = Db::name('currency')
            ->alias('a')
            ->join('currency b','a.pid=b.cu_id')
            ->field('a.title,a.cu_id,b.title as platform')
            ->where($map)
            ->select();
      //	var_dump($son);die;
        $platform = Db::name('currency')->where(['pid'=>0])->select();
        $ref  = [];
        foreach ($platform as $k=>$v){
            $sons = [];
            foreach ( $son as $ke=>$va){
                if($v['title'] == $va['platform']){
                    $va['price'] = Db::name('timelydata')->where(['title'=>$va['title'],'platform'=>$va['platform']])->value('price');
                    $va['CNY'] = $this->ChangeCNY($va['price']);
                  	$va['title'] = strtoupper($va['title']);
                    array_push($sons,$va);
                }
            }
            if(!$sons)continue;
            $ref[] = [
                'platform'=>$v['title'],
                'son'   =>$sons,
            ];
        }
        return $this->returnSuccess($ref);
    }
    //todo 高低提醒添加
    public function HeightRemindAdd(){
      	if($this->vip == 0)return $this->returnError('请先订阅会员服务');
      	if($this->vip != 2 && $this->vip_expire < time())return $this->returnError('服务已到期,请续费');
        $map = ['uid'=>$this->uid,'cu_id'=>$this->data['cu_id']];
        $ref = Db::name('height_remind')
            ->where($map)
            ->find();
      	$count = Db::name('height_remind')->where(['uid'=>$this->uid])->count();
        $msg = false;
        switch ($this->vip){
            case 1;
                if($count>=10)$msg =true;
                break;
            case 2;
                if($count>=15)$msg =true;
                break;
        }
        if($msg)return $this->returnError('设置提醒超出限制');
        if($ref)return $this->returnError('存在相同的提醒');
      	
        $res = Db::name('height_remind')->insert($map);
      	$msg =  Db::name('message_model')->where(['id'=>5])->find();
        $data = Db::name('jiage')->where(['cu_id'=>$this->data['cu_id']])->find(); 	
        $rea = $this->GetMessage($msg['content'],$msg['title'],$data);  
      
        $a = $this->SendMsg($rea,[$this->uid],5);
      	$reg_id = $this->GetPushReg($this->uid);
        if($reg_id)$this->PushMsg($rea['content'],$rea['title'],$reg_id);
        if($res)return $this->returnSuccess(null,'添加成功');
        return $this->returnError($this->Error);
    }
    //todo 高低提醒删除
    public function HeightRemindDel(){
        $id = isset($this->data['id']) ? explode(',',$this->data['id']) :false;
      	$cu_id = isset($this->data['cu_id']) ? $this->data['cu_id'] :false;
        if($id)$map = ['uid'=>$this->uid,'id'=>['in',$id]];
      	if($cu_id)$map = ['uid'=>$this->uid,'cu_id'=>$cu_id];
        $ref = Db::name('height_remind')->where($map)->select();
        if(!$ref)return $this->returnError('不存在的提醒');
        $res = Db::name('height_remind')->where($map)->delete();
        if($res)return $this->returnSuccess(null,'删除成功');
        return $this->returnError($this->Error);
    }
}